#include "msgfactory.h"
#include "txtconfig.h"
#include "smpp.h"
#include <QByteArray>

QString MsgFactory::setValue(QDomElement &item)
{
	return QString(); //???
}

QString MsgFactory::getValue(QDomElement &item)
{
	QDomAttr attr=item.attributeNode("getid");
	if ((!attr.value().isEmpty()) && (attr.value().toInt()<MSGFACTORY_PARA_NUM))
		return mParaArray[attr.value().toInt()];
	else
		return item.text();
		
}

void MsgFactory::fromStr(int type, void* &ptr, QString &txt)
{
	switch (type)
	{
		case Smpp::CommandId::Logon_req:
			gen_Logon_req(ptr,txt);
			break;
		case Smpp::CommandId::Logon_rsp:
			gen_Logon_rsp(ptr,txt);
			break;
		case Smpp::CommandId::Logout_req:
			gen_Logout_req(ptr,txt);
			break;
		case Smpp::CommandId::Logout_rsp:
			gen_Logout_rsp(ptr,txt);
			break;
		case Smpp::CommandId::Register_req:
			gen_Register_req(ptr,txt);
			break;
		case Smpp::CommandId::Register_rsp:
			gen_Register_rsp(ptr,txt);
			break;
		case Smpp::CommandId::UnRegister_req:				
			gen_UnRegister_req(ptr,txt);
			break;
		case Smpp::CommandId::UnRegister_rsp:				
			gen_UnRegister_rsp(ptr,txt);
			break;
		case Smpp::CommandId::Notify_req:
			gen_Notify_req(ptr,txt);
			break;
		case Smpp::CommandId::Notify_rsp:
			gen_Notify_rsp(ptr,txt);
			break;
		case Smpp::CommandId::Handover_req:
			gen_Handover_req(ptr,txt);
			break;
		case Smpp::CommandId::Handover_rsp:
			gen_Handover_rsp(ptr,txt);
			break;
		case Smpp::CommandId::Takeover_req:
			gen_Takeover_req(ptr,txt);
			break;
		case Smpp::CommandId::Takeover_rsp:
			gen_Takeover_rsp(ptr,txt);
			break;
		case Smpp::CommandId::Subscribe_req:
			gen_Subscribe_req(ptr,txt);
			break;
		case Smpp::CommandId::Subscribe_rsp:
			gen_Subscribe_rsp(ptr,txt);
			break;
		case Smpp::CommandId::UnSubscribe_req:
			gen_UnSubscribe_req(ptr,txt);
			break;
		case Smpp::CommandId::UnSubscribe_rsp:
			gen_UnSubscribe_rsp(ptr,txt);
			break;
		case Smpp::CommandId::ForceLogout_req:
			gen_ForceLogout_req(ptr,txt);
			break;
		case Smpp::CommandId::ForceLogout_rsp:
			gen_ForceLogout_rsp(ptr,txt);
			break;
		default:
			ptr=NULL;
	}


}

void MsgFactory::gen_Logon_req(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::LogonReq*)ptr;
	Smpp::LogonReq *pdu = new Smpp::LogonReq;
	ptr = pdu;
	
	QDomDocument doc("LogonReq");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
	 	if(!e.isNull()) 
	 	{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="OriID")
				pdu->OriID = getValue(e).toInt();
			else if (e.tagName()=="DstID")
				pdu->DstID = getValue(e).toInt();
			else if (e.tagName()=="LogonID")
				pdu->LogonID = getValue(e).toInt();
			else if (e.tagName()=="Seq")
				pdu->Seq = getValue(e).toInt();
			else if (e.tagName()=="SubmitTime")
				pdu->SubmitTime = getValue(e).toInt();
			else if (e.tagName()=="TMSInfo")
				{
					Smpp::TMSInfo info;
					QDomNode t = e.firstChild();
					while(!t.isNull()) 
					{
						QDomElement v = t.toElement(); 
						if(!v.isNull()) 
						{
							if (v.tagName()=="LogonTime")
								info.LogonTime = getValue(v).toLocal8Bit().data();
							else if (v.tagName()=="MSCode")
								info.setMSCode( getValue(v).toLocal8Bit().data());
							else if (v.tagName()=="MSType")
								info.setMSType( getValue(v).toLocal8Bit().data());
							else if (v.tagName()=="OriArea")
								info.setOriArea( getValue(v).toLocal8Bit().data());
							else if (v.tagName()=="DstArea")
								info.setDstArea( getValue(v).toLocal8Bit().data());
						}
						t = t.nextSibling();
					}
					pdu->insert_tlv(Smpp::Tlv(Smpp::Tlv::TMSInfo,(Smpp::Uint16)(info.command_length()),info.encode())); 	
				}
			else if (e.tagName()=="TFSInfo")
				{
					Smpp::TFSInfo info;
					QDomNode t = e.firstChild();
					while(!t.isNull()) 
					{
						QDomElement v = t.toElement(); 
						if(!v.isNull()) 
						{
							if (v.tagName()=="FSCode")
								info.setFSCode( getValue(v).toLocal8Bit().data());
						}
						t = t.nextSibling();
					}
					pdu->insert_tlv(Smpp::Tlv(Smpp::Tlv::TFSInfo,(Smpp::Uint16)(info.command_length()),info.encode())); 	
				}
			else if (e.tagName()=="TFSState")
				{
					Smpp::TFSState info;
					QDomNode t = e.firstChild();
					while(!t.isNull()) 
					{
						QDomElement v = t.toElement(); 
						if(!v.isNull()) 
						{
							if (v.tagName()=="FSType")
								info.FSType = getValue(v).toInt();
							else if (v.tagName()=="CCFSID")
								info.CCFSID = getValue(v).toInt();
						}
						t = t.nextSibling();
					}
					pdu->insert_tlv(Smpp::Tlv(Smpp::Tlv::TFSState,(Smpp::Uint16)(info.command_length()),info.encode()));		
				}
			else if (e.tagName()=="TServInfo")
				{
					Smpp::TServInfo info;
					int i=0;
					QDomNode t = e.firstChild();
					while(!t.isNull()) 
					{
						QDomElement v = t.toElement(); 
						if(!v.isNull()) 
						{
							if (v.tagName()=="ServiceNum")
								info.setServiceNum(getValue(v).toInt());
							else if (v.tagName()=="Service")
								info.Service[i++] = getValue(v).toInt();
						}
						t = t.nextSibling();
					}
					info.setServiceNum(i);
					pdu->insert_tlv(Smpp::Tlv(Smpp::Tlv::TServInfo,(Smpp::Uint16)(info.command_length()),info.encode()));		
				}
	 
		}
		n = n.nextSibling();
	}

}

void MsgFactory::gen_Logon_rsp(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::LogonRsp*)ptr;
	Smpp::LogonRsp *pdu = new Smpp::LogonRsp;
	ptr = pdu;
	
	QDomDocument doc("LogonRsp");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
	 	if(!e.isNull()) 
	 	{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="CommandStatus")
				pdu->command_status(getValue(e).toInt());
			else if (e.tagName()=="LogonID")
				pdu->LogonID= getValue(e).toInt();
			else if (e.tagName()=="Seq")
				pdu->Seq= getValue(e).toInt();
			else if (e.tagName()=="AcceptTime")
				pdu->AcceptTime= getValue(e).toInt();
			else if (e.tagName()=="CCFSID")
				pdu->CCFSID= getValue(e).toInt();
			else if (e.tagName()=="TFSInfo")
				{
					Smpp::TFSInfo info;
					QDomNode t = e.firstChild();
					while(!t.isNull()) 
					{
						QDomElement v = t.toElement(); 
						if(!v.isNull()) 
						{
							if (v.tagName()=="FSCode")
								info.setFSCode( getValue(v).toLocal8Bit().data());
						}
						t = t.nextSibling();
					}
					pdu->insert_tlv(Smpp::Tlv(Smpp::Tlv::TFSInfo,(Smpp::Uint16)(info.command_length()),info.encode())); 	
				}
			else if (e.tagName()=="TServInfo")
				{
					Smpp::TServInfo info;
					int i=0;
					QDomNode t = e.firstChild();
					while(!t.isNull()) 
					{
						QDomElement v = t.toElement(); 
						if(!v.isNull()) 
						{
							if (v.tagName()=="ServiceNum")
								info.setServiceNum(getValue(v).toInt());
							else if (v.tagName()=="Service")
								info.Service[i++] = getValue(v).toInt();
						}
						t = t.nextSibling();
					}
					info.setServiceNum(i);
					pdu->insert_tlv(Smpp::Tlv(Smpp::Tlv::TServInfo,(Smpp::Uint16)(info.command_length()),info.encode()));		
				}
		}
		n = n.nextSibling();
	}
}
void MsgFactory::gen_Logout_req(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::LogoutReq*)ptr;
	Smpp::LogoutReq *pdu = new Smpp::LogoutReq;
	ptr = pdu;
	
	QDomDocument doc("LogoutReq");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
	 	if(!e.isNull()) 
	 	{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="OriID")
				pdu->OriID = getValue(e).toInt();
			else if (e.tagName()=="DstID")
				pdu->DstID = getValue(e).toInt();
			else if (e.tagName()=="LogoutID")
				pdu->LogoutID = getValue(e).toInt();
			else if (e.tagName()=="Seq")
				pdu->Seq = getValue(e).toInt();
			else if (e.tagName()=="SubmitTime")
				pdu->SubmitTime = getValue(e).toInt();
		}
		n = n.nextSibling();
	}

}
void MsgFactory::gen_Logout_rsp(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::LogoutRsp*)ptr;
	Smpp::LogoutRsp *pdu = new Smpp::LogoutRsp;
	ptr = pdu;
	
	QDomDocument doc("LogoutRsp");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
	 	if(!e.isNull()) 
	 	{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="CommandStatus")
				pdu->command_status(getValue(e).toInt());
			else if (e.tagName()=="LogoutID")
				pdu->LogoutID = getValue(e).toInt();
			else if (e.tagName()=="Seq")
				pdu->Seq= getValue(e).toInt();
			else if (e.tagName()=="AcceptTime")
				pdu->AcceptTime= getValue(e).toInt();
		}
		n = n.nextSibling();
	}

}
void MsgFactory::gen_Register_req(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::RegisterReq*)ptr;
	Smpp::RegisterReq *pdu = new Smpp::RegisterReq;
	ptr = pdu;
	
	QDomDocument doc("RegisterReq");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
	 	if(!e.isNull()) 
	 	{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="OriID")
				pdu->OriID = getValue(e).toInt();
			else if (e.tagName()=="DstID")
				pdu->DstID = getValue(e).toInt();
			else if (e.tagName()=="RegID")
				pdu->RegID= getValue(e).toInt();
			else if (e.tagName()=="TBearAddr")
				{
					Smpp::TBearAddr info;
					QDomNode t = e.firstChild();
					while(!t.isNull()) 
					{
						QDomElement v = t.toElement(); 
						if(!v.isNull()) 
						{
							if (v.tagName()=="IPAddr")
								info.setIPAddr( getValue(v).toLocal8Bit().data());
							else if (v.tagName()=="Port")
								info.Port = getValue(v).toInt();
						}
						t = t.nextSibling();
					}
					pdu->insert_tlv(Smpp::Tlv(Smpp::Tlv::TBearAddr,(Smpp::Uint16)(info.command_length()),info.encode())); 	
				}
		}
		n = n.nextSibling();
	}

}
void MsgFactory::gen_Register_rsp(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::RegisterRsp*)ptr;
	Smpp::RegisterRsp *pdu = new Smpp::RegisterRsp;
	ptr = pdu;
	
	QDomDocument doc("RegisterRsp");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
		if(!e.isNull()) 
		{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="CommandStatus")
				pdu->command_status(getValue(e).toInt());
			else if (e.tagName()=="RegID")
				pdu->RegID = getValue(e).toInt();
			else if (e.tagName()=="TMSInfo")
				{
					Smpp::TMSInfo info;
					QDomNode t = e.firstChild();
					while(!t.isNull()) 
					{
						QDomElement v = t.toElement(); 
						if(!v.isNull()) 
						{
							if (v.tagName()=="LogonTime")
								info.LogonTime = getValue(v).toLocal8Bit().data();
							else if (v.tagName()=="MSCode")
								info.setMSCode( getValue(v).toLocal8Bit().data());
							else if (v.tagName()=="MSType")
								info.setMSType( getValue(v).toLocal8Bit().data());
							else if (v.tagName()=="OriArea")
								info.setOriArea( getValue(v).toLocal8Bit().data());
							else if (v.tagName()=="DstArea")
								info.setDstArea( getValue(v).toLocal8Bit().data());
						}
						t = t.nextSibling();
					}
					pdu->insert_tlv(Smpp::Tlv(Smpp::Tlv::TMSInfo,(Smpp::Uint16)(info.command_length()),info.encode())); 	
				}
			else if (e.tagName()=="TServInfo")
				{
					Smpp::TServInfo info;
					int i=0;
					QDomNode t = e.firstChild();
					while(!t.isNull()) 
					{
						QDomElement v = t.toElement(); 
						if(!v.isNull()) 
						{
							if (v.tagName()=="ServiceNum")
								info.setServiceNum(getValue(v).toInt());
							else if (v.tagName()=="Service")
								info.Service[i++] = getValue(v).toInt();
						}
						t = t.nextSibling();
					}
					info.setServiceNum(i);
					pdu->insert_tlv(Smpp::Tlv(Smpp::Tlv::TServInfo,(Smpp::Uint16)(info.command_length()),info.encode()));		
				}
			else if (e.tagName()=="TMSSubscription")
				{
					Smpp::TMSSubscription info;
					int i=0;
					QDomNode t = e.firstChild();
					while(!t.isNull()) 
					{
						QDomElement v = t.toElement(); 
						if(!v.isNull()) 
						{
							if (v.tagName()=="MSID")
								info.MSID=getValue(v).toInt();
							else if (v.tagName()=="FSIDNum")
								info.setFSIDNum(getValue(v).toInt());
							else if (v.tagName()=="FSID")
								info.FSID[i++]=getValue(v).toInt();
						}
						t = t.nextSibling();
					}
					info.setFSIDNum(i);
					pdu->insert_tlv(Smpp::Tlv(Smpp::Tlv::TMSSubscription,(Smpp::Uint16)(info.command_length()),info.encode())); 	
				}
		}
		n = n.nextSibling();
	}

}

void MsgFactory::gen_UnRegister_req(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::UnRegisterReq*)ptr;
	Smpp::UnRegisterReq *pdu = new Smpp::UnRegisterReq;
	ptr = pdu;
	
	QDomDocument doc("UnRegisterReq");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
	 	if(!e.isNull()) 
	 	{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="OriID")
				pdu->OriID = getValue(e).toInt();
			else if (e.tagName()=="DstID")
				pdu->DstID = getValue(e).toInt();
			else if (e.tagName()=="UnRegID")
				pdu->UnRegID= getValue(e).toInt();
		}
		n = n.nextSibling();
	}

}
void MsgFactory::gen_UnRegister_rsp(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::UnRegisterRsp*)ptr;
	Smpp::UnRegisterRsp *pdu = new Smpp::UnRegisterRsp;
	ptr = pdu;
	
	QDomDocument doc("UnRegisterRsp");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
	 	if(!e.isNull()) 
	 	{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="CommandStatus")
				pdu->command_status(getValue(e).toInt());
		}
		n = n.nextSibling();
	}

}
void MsgFactory::gen_Notify_req(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::NotifyReq*)ptr;
	Smpp::NotifyReq *pdu = new Smpp::NotifyReq;
	ptr = pdu;
	
	QDomDocument doc("NotifyReq");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
	 	if(!e.isNull()) 
	 	{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="OriID")
				pdu->OriID = getValue(e).toInt();
			else if (e.tagName()=="DstID")
				pdu->DstID = getValue(e).toInt();
			else if (e.tagName()=="NotifyID")
				pdu->NotifyID= getValue(e).toInt();
			else if (e.tagName()=="SyncTag")
				pdu->SyncTag= getValue(e).toInt();
			else if (e.tagName()=="TStatuesInfo")
				{
					Smpp::TStatuesInfo info;
					QDomNode t = e.firstChild();
					while(!t.isNull()) 
					{
						QDomElement v = t.toElement(); 
						if(!v.isNull()) 
						{
							if (v.tagName()=="Action")
								info.Action = getValue(v).toInt();
							else if (v.tagName()=="TFSDescription")
								{
									Smpp::TFSDriscription info_tfs;
									QDomNode w = v.firstChild();
									while(!w.isNull()) 
									{
										QDomElement x = w.toElement(); 
										if(!x.isNull()) 
										{
											if (x.tagName()=="FSID")
												info_tfs.FSID= getValue(x).toInt();
											else if (x.tagName()=="TFSInfo")
												{
													Smpp::TFSInfo fsinfo;
													QDomNode y = x.firstChild();
													while(!y.isNull()) 
													{
														QDomElement z = y.toElement(); 
														if(!z.isNull()) 
														{
															if (z.tagName()=="FSCode")
																fsinfo.setFSCode( getValue(z).toLocal8Bit().data());
														}
														y = y.nextSibling();
													}
													info_tfs.insert_tlv(Smpp::Tlv(Smpp::Tlv::TFSInfo,(Smpp::Uint16)(fsinfo.command_length()),fsinfo.encode())); 	
												}
											else if (x.tagName()=="TBearAddr")
												{
													Smpp::TBearAddr fsaddr;
													QDomNode y = x.firstChild();
													while(!y.isNull()) 
													{
														QDomElement z = y.toElement(); 
														if(!z.isNull()) 
														{
															if (z.tagName()=="IPAddr")
																fsaddr.setIPAddr( getValue(z).toLocal8Bit().data());
															else if (z.tagName()=="Port")
																fsaddr.Port = getValue(z).toInt();
														}
														y = y.nextSibling();
													}
													info_tfs.insert_tlv(Smpp::Tlv(Smpp::Tlv::TBearAddr,(Smpp::Uint16)(fsaddr.command_length()),fsaddr.encode()));	
												}
											else if (x.tagName()=="TServInfo")
												{
													Smpp::TServInfo fsserv;
													int i=0;
													QDomNode y = x.firstChild();
													while(!y.isNull()) 
													{
														QDomElement z = y.toElement(); 
														if(!z.isNull()) 
														{
															if (z.tagName()=="ServiceNum")
																fsserv.setServiceNum(getValue(z).toInt());
															else if (z.tagName()=="Service")
																fsserv.Service[i++] = getValue(z).toInt();
														}
														y = y.nextSibling();
													}
													fsserv.setServiceNum(i);
													info_tfs.insert_tlv(Smpp::Tlv(Smpp::Tlv::TServInfo,(Smpp::Uint16)(fsserv.command_length()),fsserv.encode()));		
												}
											else if (x.tagName()=="TFSState")
												{
													Smpp::TFSState fsstate;
													QDomNode y = x.firstChild();
													while(!y.isNull()) 
													{
														QDomElement z = y.toElement(); 
														if(!z.isNull()) 
														{
															if (z.tagName()=="FSType")
																fsstate.FSType = getValue(z).toInt();
															else if (z.tagName()=="CCFSID")
																fsstate.CCFSID = getValue(z).toInt();
														}
														y = y.nextSibling();
													}
													info_tfs.insert_tlv(Smpp::Tlv(Smpp::Tlv::TFSState,(Smpp::Uint16)(fsstate.command_length()),fsstate.encode()));		
												}
										}
										w = w.nextSibling();
									}
									info.insert_tlv(Smpp::Tlv(Smpp::Tlv::TFSDriscription,(Smpp::Uint16)(info_tfs.command_length()),info_tfs.encode())); 	
								}
							else if (v.tagName()=="TMSDescription")
								{
									Smpp::TMSDriscription info_tms;
									QDomNode w = v.firstChild();
									while(!w.isNull()) 
									{
										QDomElement x = w.toElement(); 
										if(!x.isNull()) 
										{
											if (x.tagName()=="MSID")
												info_tms.MSID= getValue(x).toInt();
											else if (x.tagName()=="TMSInfo")
												{
													Smpp::TMSInfo msinfo;
													QDomNode y = x.firstChild();
													while(!y.isNull()) 
													{
														QDomElement z = y.toElement(); 
														if(!z.isNull()) 
														{
															if (z.tagName()=="LogonTime")
															msinfo.LogonTime=getValue(z).toLocal8Bit().data();
															else if (z.tagName()=="MSCode")
															msinfo.setMSCode( getValue(z).toLocal8Bit().data());
															else if (z.tagName()=="MSType")
															msinfo.setMSType( getValue(z).toLocal8Bit().data());
															else if (z.tagName()=="OriArea")
															msinfo.setOriArea( getValue(z).toLocal8Bit().data());
															else if (z.tagName()=="DstArea")
															msinfo.setDstArea( getValue(z).toLocal8Bit().data());
														}
														y = y.nextSibling();
													}
													info_tms.insert_tlv(Smpp::Tlv(Smpp::Tlv::TFSInfo,(Smpp::Uint16)(msinfo.command_length()),msinfo.encode())); 	
												}
											else if (x.tagName()=="TBearAddr")
												{
													Smpp::TBearAddr msaddr;
													QDomNode y = x.firstChild();
													while(!y.isNull()) 
													{
														QDomElement z = y.toElement(); 
														if(!z.isNull()) 
														{
															if (z.tagName()=="IPAddr")
																msaddr.setIPAddr( getValue(z).toLocal8Bit().data());
															else if (z.tagName()=="Port")
																msaddr.Port = getValue(z).toInt();
														}
														y = y.nextSibling();
													}
													info_tms.insert_tlv(Smpp::Tlv(Smpp::Tlv::TBearAddr,(Smpp::Uint16)(msaddr.command_length()),msaddr.encode()));	
												}
											else if (x.tagName()=="TServInfo")
												{
													Smpp::TServInfo msserv;
													int i=0;
													QDomNode y = x.firstChild();
													while(!y.isNull()) 
													{
														QDomElement z = y.toElement(); 
														if(!z.isNull()) 
														{
															if (z.tagName()=="ServiceNum")
																msserv.setServiceNum(getValue(z).toInt());
															else if (z.tagName()=="Service")
																msserv.Service[i++] = getValue(z).toInt();
														}
														y = y.nextSibling();
													}
													msserv.setServiceNum(i);
													info_tms.insert_tlv(Smpp::Tlv(Smpp::Tlv::TServInfo,(Smpp::Uint16)(msserv.command_length()),msserv.encode()));		
												}
											else if (x.tagName()=="TMSState")
												{
													Smpp::TMSState msstate;
													QDomNode y = x.firstChild();
													while(!y.isNull()) 
													{
														QDomElement z = y.toElement(); 
														if(!z.isNull()) 
														{
															if (z.tagName()=="CurMSCID")
																msstate.CurMSCID = getValue(z).toInt();
															else if (z.tagName()=="MSState")
																msstate.MSState = getValue(z).toInt();
														}
														y = y.nextSibling();
													}
													info_tms.insert_tlv(Smpp::Tlv(Smpp::Tlv::TMSState,(Smpp::Uint16)(msstate.command_length()),msstate.encode()));		
												}
										}
										w = w.nextSibling();
									}
									info.insert_tlv(Smpp::Tlv(Smpp::Tlv::TMSDriscription,(Smpp::Uint16)(info_tms.command_length()),info_tms.encode())); 	
								}
						}
						t = t.nextSibling();
					}
					pdu->insert_tlv(Smpp::Tlv(Smpp::Tlv::TBearAddr,(Smpp::Uint16)(info.command_length()),info.encode()));	
				}
		}
		n = n.nextSibling();
	}

}
void MsgFactory::gen_Notify_rsp(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::NotifyRsp*)ptr;
	Smpp::NotifyRsp *pdu = new Smpp::NotifyRsp;
	ptr = pdu;
	
	QDomDocument doc("NotifyRsp");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
		if(!e.isNull()) 
		{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="CommandStatus")
				pdu->command_status(getValue(e).toInt());
			else if (e.tagName()=="TFSDriscription")
			{
				Smpp::TFSDriscription info_tfs;
				QDomNode w = e.firstChild();
				while(!w.isNull()) 
				{
					QDomElement x = w.toElement(); 
					if(!x.isNull()) 
					{
						if (x.tagName()=="FSID")
							info_tfs.FSID= getValue(x).toInt();
						else if (x.tagName()=="TFSInfo")
							{
								Smpp::TFSInfo fsinfo;
								QDomNode y = x.firstChild();
								while(!y.isNull()) 
								{
									QDomElement z = y.toElement(); 
									if(!z.isNull()) 
									{
										if (z.tagName()=="FSCode")
											fsinfo.setFSCode( getValue(z).toLocal8Bit().data());
									}
									y = y.nextSibling();
								}
								info_tfs.insert_tlv(Smpp::Tlv(Smpp::Tlv::TFSInfo,(Smpp::Uint16)(fsinfo.command_length()),fsinfo.encode()));	
							}
						else if (x.tagName()=="TBearAddr")
							{
								Smpp::TBearAddr fsaddr;
								QDomNode y = x.firstChild();
								while(!y.isNull()) 
								{
									QDomElement z = y.toElement(); 
									if(!z.isNull()) 
									{
										if (z.tagName()=="IPAddr")
											fsaddr.setIPAddr( getValue(z).toLocal8Bit().data());
										else if (z.tagName()=="Port")
											fsaddr.Port = getValue(z).toInt();
									}
									y = y.nextSibling();
								}
								info_tfs.insert_tlv(Smpp::Tlv(Smpp::Tlv::TBearAddr,(Smpp::Uint16)(fsaddr.command_length()),fsaddr.encode()));	
							}
						else if (x.tagName()=="TServInfo")
							{
								Smpp::TServInfo fsserv;
								int i=0;
								QDomNode y = x.firstChild();
								while(!y.isNull()) 
								{
									QDomElement z = y.toElement(); 
									if(!z.isNull()) 
									{
										if (z.tagName()=="ServiceNum")
											fsserv.setServiceNum(getValue(z).toInt());
										else if (z.tagName()=="Service")
											fsserv.Service[i++] = getValue(z).toInt();
									}
									y = y.nextSibling();
								}
								fsserv.setServiceNum(i);
								info_tfs.insert_tlv(Smpp::Tlv(Smpp::Tlv::TServInfo,(Smpp::Uint16)(fsserv.command_length()),fsserv.encode()));		
							}
						else if (x.tagName()=="TFSState")
							{
								Smpp::TFSState fsstate;
								QDomNode y = x.firstChild();
								while(!y.isNull()) 
								{
									QDomElement z = y.toElement(); 
									if(!z.isNull()) 
									{
										if (z.tagName()=="FSType")
											fsstate.FSType = getValue(z).toInt();
										else if (z.tagName()=="CCFSID")
											fsstate.CCFSID = getValue(z).toInt();
									}
									y = y.nextSibling();
								}
								info_tfs.insert_tlv(Smpp::Tlv(Smpp::Tlv::TFSState,(Smpp::Uint16)(fsstate.command_length()),fsstate.encode())); 	
							}
					}
					w = w.nextSibling();
				}
				pdu->insert_tlv(Smpp::Tlv(Smpp::Tlv::TFSDriscription,(Smpp::Uint16)(info_tfs.command_length()),info_tfs.encode())); 	
			}
			else if (e.tagName()=="TMSDescription")
				{
					Smpp::TMSDriscription info_tms;
					QDomNode w = e.firstChild();
					while(!w.isNull()) 
					{
						QDomElement x = w.toElement(); 
						if(!x.isNull()) 
						{
							if (x.tagName()=="MSID")
								info_tms.MSID= getValue(x).toInt();
							else if (x.tagName()=="TMSInfo")
								{
									Smpp::TMSInfo msinfo;
									QDomNode y = x.firstChild();
									while(!y.isNull()) 
									{
										QDomElement z = y.toElement(); 
										if(!z.isNull()) 
										{
											if (x.tagName()=="LogonTime")
											msinfo.LogonTime=getValue(z).toLocal8Bit().data();
											else if (z.tagName()=="MSCode")
											msinfo.setMSCode( getValue(z).toLocal8Bit().data());
											else if (z.tagName()=="MSType")
											msinfo.setMSType( getValue(z).toLocal8Bit().data());
											else if (z.tagName()=="OriArea")
											msinfo.setOriArea( getValue(z).toLocal8Bit().data());
											else if (z.tagName()=="DstArea")
											msinfo.setDstArea( getValue(z).toLocal8Bit().data());
										}
										y = y.nextSibling();
									}
									info_tms.insert_tlv(Smpp::Tlv(Smpp::Tlv::TFSInfo,(Smpp::Uint16)(msinfo.command_length()),msinfo.encode()));	
								}
							else if (x.tagName()=="TBearAddr")
								{
									Smpp::TBearAddr msaddr;
									QDomNode y = x.firstChild();
									while(!y.isNull()) 
									{
										QDomElement z = y.toElement(); 
										if(!z.isNull()) 
										{
											if (z.tagName()=="IPAddr")
												msaddr.setIPAddr( getValue(z).toLocal8Bit().data());
											else if (z.tagName()=="Port")
												msaddr.Port = getValue(z).toInt();
										}
										y = y.nextSibling();
									}
									info_tms.insert_tlv(Smpp::Tlv(Smpp::Tlv::TBearAddr,(Smpp::Uint16)(msaddr.command_length()),msaddr.encode()));	
								}
							else if (x.tagName()=="TServInfo")
								{
									Smpp::TServInfo msserv;
									int i=0;
									QDomNode y = x.firstChild();
									while(!y.isNull()) 
									{
										QDomElement z = y.toElement(); 
										if(!z.isNull()) 
										{
											if (z.tagName()=="ServiceNum")
												msserv.setServiceNum(getValue(z).toInt());
											else if (z.tagName()=="Service")
												msserv.Service[i++] = getValue(z).toInt();
										}
										y = y.nextSibling();
									}
									msserv.setServiceNum(i);
									info_tms.insert_tlv(Smpp::Tlv(Smpp::Tlv::TServInfo,(Smpp::Uint16)(msserv.command_length()),msserv.encode()));		
								}
							else if (x.tagName()=="TMSState")
								{
									Smpp::TMSState msstate;
									QDomNode y = x.firstChild();
									while(!y.isNull()) 
									{
										QDomElement z = y.toElement(); 
										if(!z.isNull()) 
										{
											if (z.tagName()=="CurMSCID")
												msstate.CurMSCID = getValue(z).toInt();
											else if (z.tagName()=="MSState")
												msstate.MSState = getValue(z).toInt();
										}
										y = y.nextSibling();
									}
									info_tms.insert_tlv(Smpp::Tlv(Smpp::Tlv::TMSState,(Smpp::Uint16)(msstate.command_length()),msstate.encode())); 	
								}
						}
						w = w.nextSibling();
					}
					pdu->insert_tlv(Smpp::Tlv(Smpp::Tlv::TMSDriscription,(Smpp::Uint16)(info_tms.command_length()),info_tms.encode())); 	
				}
		}
		n = n.nextSibling();
	}

}
void MsgFactory::gen_Handover_req(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::HandoverReq*)ptr;
	Smpp::HandoverReq *pdu = new Smpp::HandoverReq;
	ptr = pdu;
	
	QDomDocument doc("HandoverReq");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
	 	if(!e.isNull()) 
	 	{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="OriID")
				pdu->OriID = getValue(e).toInt();
			else if (e.tagName()=="DstID")
				pdu->DstID = getValue(e).toInt();
			else if (e.tagName()=="HandoverID")
				pdu->HandoverID = getValue(e).toInt();
		}
		n = n.nextSibling();
	}

}
void MsgFactory::gen_Handover_rsp(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::HandoverRsp*)ptr;
	Smpp::HandoverRsp *pdu = new Smpp::HandoverRsp;
	ptr = pdu;
	
	QDomDocument doc("HandoverRsp");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
		if(!e.isNull()) 
		{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="CommandStatus")
				pdu->command_status(getValue(e).toInt());
			else if (e.tagName()=="HandoverID")
				pdu->HandoverID= getValue(e).toInt();
		}
		n = n.nextSibling();
	}

}
void MsgFactory::gen_Takeover_req(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::TakeoverReq*)ptr;
	Smpp::TakeoverReq *pdu = new Smpp::TakeoverReq;
	ptr = pdu;
	
	QDomDocument doc("TakeoverReq");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
	 	if(!e.isNull()) 
	 	{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="OriID")
				pdu->OriID = getValue(e).toInt();
			else if (e.tagName()=="DstID")
				pdu->DstID = getValue(e).toInt();
			else if (e.tagName()=="TakeoverID")
				pdu->TakeoverID = getValue(e).toInt();
			else if (e.tagName()=="TMSSubscription")
				{
					Smpp::TMSSubscription info;
					int i=0;
					QDomNode t = e.firstChild();
					while(!t.isNull()) 
					{
						QDomElement v = t.toElement(); 
						if(!v.isNull()) 
						{
							if (v.tagName()=="MSID")
								info.MSID=getValue(v).toInt();
							else if (v.tagName()=="FSIDNum")
								info.setFSIDNum(getValue(v).toInt());
							else if (v.tagName()=="FSID")
								info.FSID[i++]=getValue(v).toInt();
						}
						t = t.nextSibling();
					}
					info.setFSIDNum(i);
					pdu->insert_tlv(Smpp::Tlv(Smpp::Tlv::TMSSubscription,(Smpp::Uint16)(info.command_length()),info.encode())); 	
				}
		}
		n = n.nextSibling();
	}

}
void MsgFactory::gen_Takeover_rsp(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::TakeoverRsp*)ptr;
	Smpp::TakeoverRsp *pdu = new Smpp::TakeoverRsp;
	ptr = pdu;
	
	QDomDocument doc("TakeoverRsp");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
	 	if(!e.isNull()) 
	 	{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="CommandStatus")
				pdu->command_status(getValue(e).toInt());
			else if (e.tagName()=="TakeoverID")
				pdu->TakeoverID = getValue(e).toInt();
		}
		n = n.nextSibling();
	}

}
void MsgFactory::gen_Subscribe_req(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::SubscribeReq*)ptr;
	Smpp::SubscribeReq *pdu = new Smpp::SubscribeReq;
	ptr = pdu;
	
	QDomDocument doc("SubscribeReq");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
	 	if(!e.isNull()) 
	 	{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="OriID")
				pdu->OriID = getValue(e).toInt();
			else if (e.tagName()=="DstID")
				pdu->DstID = getValue(e).toInt();
			else if (e.tagName()=="SubscribeID")
				pdu->SubscribeID = getValue(e).toInt();
			else if (e.tagName()=="FSID")
				pdu->FSID = getValue(e).toInt();
		}
		n = n.nextSibling();
	}

}
void MsgFactory::gen_Subscribe_rsp(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::SubscribeRsp*)ptr;
	Smpp::SubscribeRsp *pdu = new Smpp::SubscribeRsp;
	ptr = pdu;
	
	QDomDocument doc("SubscribeRsp");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
	 	if(!e.isNull()) 
	 	{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="CommandStatus")
				pdu->command_status(getValue(e).toInt());
			else if (e.tagName()=="SubscribeID")
				pdu->SubscribeID = getValue(e).toInt();
			else if (e.tagName()=="TMSInfo")
				{
					Smpp::TMSInfo info;
					QDomNode t = e.firstChild();
					while(!t.isNull()) 
					{
						QDomElement v = t.toElement(); 
						if(!v.isNull()) 
						{
							if (v.tagName()=="LogonTime")
								info.LogonTime = getValue(v).toLocal8Bit().data();
							else if (v.tagName()=="MSCode")
								info.setMSCode( getValue(v).toLocal8Bit().data());
							else if (v.tagName()=="MSType")
								info.setMSType( getValue(v).toLocal8Bit().data());
							else if (v.tagName()=="OriArea")
								info.setOriArea( getValue(v).toLocal8Bit().data());
							else if (v.tagName()=="DstArea")
								info.setDstArea( getValue(v).toLocal8Bit().data());
						}
						t = t.nextSibling();
					}
					pdu->insert_tlv(Smpp::Tlv(Smpp::Tlv::TMSInfo,(Smpp::Uint16)(info.command_length()),info.encode())); 	
				}
			else if (e.tagName()=="TServInfo")
				{
					Smpp::TServInfo info;
					int i=0;
					QDomNode t = e.firstChild();
					while(!t.isNull()) 
					{
						QDomElement v = t.toElement(); 
						if(!v.isNull()) 
						{
							if (v.tagName()=="ServiceNum")
								info.setServiceNum(getValue(v).toInt());
							else if (v.tagName()=="Service")
								info.Service[i++] = getValue(v).toInt();
						}
						t = t.nextSibling();
					}
					info.setServiceNum(i);
					pdu->insert_tlv(Smpp::Tlv(Smpp::Tlv::TServInfo,(Smpp::Uint16)(info.command_length()),info.encode()));		
				}
			else if (e.tagName()=="TMSState")
				{
					Smpp::TMSState info;
					QDomNode t = e.firstChild();
					while(!t.isNull()) 
					{
						QDomElement v = t.toElement(); 
						if(!v.isNull()) 
						{
							if (v.tagName()=="CurMSCID")
								info.CurMSCID = getValue(v).toInt();
							else if (v.tagName()=="MSState")
								info.MSState = getValue(v).toInt();
						}
						t = t.nextSibling();
					}
					pdu->insert_tlv(Smpp::Tlv(Smpp::Tlv::TMSState,(Smpp::Uint16)(info.command_length()),info.encode())); 	
				}
		}
		n = n.nextSibling();
	}

}
void MsgFactory::gen_UnSubscribe_req(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::UnSubscribeReq*)ptr;
	Smpp::UnSubscribeReq *pdu = new Smpp::UnSubscribeReq;
	ptr = pdu;
	
	QDomDocument doc("UnSubscribeReq");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
	 	if(!e.isNull()) 
	 	{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="OriID")
				pdu->OriID = getValue(e).toInt();
			else if (e.tagName()=="DstID")
				pdu->DstID = getValue(e).toInt();
			else if (e.tagName()=="SubscribeID")
				pdu->SubscribeID = getValue(e).toInt();
			else if (e.tagName()=="FSID")
				pdu->FSID = getValue(e).toInt();
		}
		n = n.nextSibling();
	}

}
void MsgFactory::gen_UnSubscribe_rsp(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::UnSubscribeRsp*)ptr;
	Smpp::UnSubscribeRsp *pdu = new Smpp::UnSubscribeRsp;
	ptr = pdu;
	
	QDomDocument doc("UnSubscribeRsp");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
	 	if(!e.isNull()) 
	 	{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="CommandStatus")
				pdu->command_status(getValue(e).toInt());
		}
		n = n.nextSibling();
	}

}
void MsgFactory::gen_ForceLogout_req(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::ForceLogoutReq*)ptr;
	Smpp::ForceLogoutReq *pdu = new Smpp::ForceLogoutReq;
	ptr = pdu;
	
	QDomDocument doc("ForceLogoutReq");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
	 	if(!e.isNull()) 
	 	{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="OriID")
				pdu->OriID = getValue(e).toInt();
			else if (e.tagName()=="DstID")
				pdu->DstID = getValue(e).toInt();
			else if (e.tagName()=="SubscribeID")
				pdu->SubscribeID = getValue(e).toInt();
			else if (e.tagName()=="FSID")
				pdu->FSID = getValue(e).toInt();
		}
		n = n.nextSibling();
	}

}
void MsgFactory::gen_ForceLogout_rsp(void* &ptr, QString &txt)
{
	if (ptr)
		delete (Smpp::ForceLogoutRsp*)ptr;
	Smpp::ForceLogoutRsp *pdu = new Smpp::ForceLogoutRsp;
	ptr = pdu;
	
	QDomDocument doc("ForceLogoutRsp");	
	doc.setContent(txt);
	
	QDomElement docElem = doc.documentElement();
	
	QDomNode n = docElem.firstChild();
	while(!n.isNull()) 
	{
		QDomElement e = n.toElement(); 
	 	if(!e.isNull()) 
	 	{
			if (e.tagName()=="SequenceNumber")
				pdu->sequence_number(getValue(e).toInt());
			else if (e.tagName()=="CommandStatus")
				pdu->command_status(getValue(e).toInt());
		}
		n = n.nextSibling();
	}

}

QString MsgFactory::toStr(int type, void* &ptr)
{
	QString txt;
	switch (type)
	{
		case Smpp::CommandId::Logon_req:
			str_Logon_req(ptr,txt);
			break;
		case Smpp::CommandId::Logon_rsp:
			str_Logon_rsp(ptr,txt);
			break;
		case Smpp::CommandId::Logout_req:
			str_Logout_req(ptr,txt);
			break;
		case Smpp::CommandId::Logout_rsp:
			str_Logout_rsp(ptr,txt);
			break;
		case Smpp::CommandId::Register_req:
			str_Register_req(ptr,txt);
			break;
		case Smpp::CommandId::Register_rsp:
			str_Register_rsp(ptr,txt);
			break;
		case Smpp::CommandId::UnRegister_req:				
			str_UnRegister_req(ptr,txt);
			break;
		case Smpp::CommandId::UnRegister_rsp:				
			str_UnRegister_rsp(ptr,txt);
			break;
		case Smpp::CommandId::Notify_req:
			str_Notify_req(ptr,txt);
			break;
		case Smpp::CommandId::Notify_rsp:
			str_Notify_rsp(ptr,txt);
			break;
		case Smpp::CommandId::Handover_req:
			str_Handover_req(ptr,txt);
			break;
		case Smpp::CommandId::Handover_rsp:
			str_Handover_rsp(ptr,txt);
			break;
		case Smpp::CommandId::Takeover_req:
			str_Takeover_req(ptr,txt);
			break;
		case Smpp::CommandId::Takeover_rsp:
			str_Takeover_rsp(ptr,txt);
			break;
		case Smpp::CommandId::Subscribe_req:
			str_Subscribe_req(ptr,txt);
			break;
		case Smpp::CommandId::Subscribe_rsp:
			str_Subscribe_rsp(ptr,txt);
			break;
		case Smpp::CommandId::UnSubscribe_req:
			str_UnSubscribe_req(ptr,txt);
			break;
		case Smpp::CommandId::UnSubscribe_rsp:
			str_UnSubscribe_rsp(ptr,txt);
			break;
		case Smpp::CommandId::ForceLogout_req:
			str_ForceLogout_req(ptr,txt);
			break;
		case Smpp::CommandId::ForceLogout_rsp:
			str_ForceLogout_rsp(ptr,txt);
			break;
	}

	return txt;

}

void MsgFactory::str_Logon_req(void *ptr, QString &txt)
{
	Smpp::LogonReq *pdu = (Smpp::LogonReq*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("LogonReq");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	addElement(doc, root, "OriID", QString::number(pdu->OriID.value()));
	addElement(doc, root, "DstID", QString::number(pdu->DstID.value()));
	addElement(doc, root, "LogonID", QString::number(pdu->LogonID.value()));
	addElement(doc, root, "Seq", QString::number(pdu->Seq.value()));
	addElement(doc, root, "SubmitTime", QString::number(pdu->SubmitTime.value()));

	const Smpp::Tlv* msinfo = pdu->find_tlv(Smpp::Tlv::TMSInfo);
	if(msinfo)
	{
		Smpp::TMSInfo info;
		info.decode(msinfo->value(),msinfo->length());
		QDomElement tlv=addElement(doc, root, "TMSInfo", "");
		addElement(doc, tlv, "LogonTime", info.LogonTime.c_str());
		addElement(doc, tlv, "MSCode", info.MSCode.c_str());
		addElement(doc, tlv, "MSType", info.MSType.c_str());
		addElement(doc, tlv, "OriArea", info.OriArea.c_str());
		addElement(doc, tlv, "DstArea", info.DstArea.c_str());
	}	
	
	const Smpp::Tlv* fsinfo = pdu->find_tlv(Smpp::Tlv::TFSInfo);
	if(fsinfo)
	{
		Smpp::TFSInfo info;
		info.decode(fsinfo->value(),fsinfo->length());
		QDomElement tlv=addElement(doc, root, "TFSInfo", "");
		addElement(doc, tlv, "FSCode", info.FSCode.c_str());
	}	
	
	const Smpp::Tlv* servinfo = pdu->find_tlv(Smpp::Tlv::TServInfo);
	if(servinfo)
	{
		Smpp::TServInfo info;
		info.decode(servinfo->value(),servinfo->length());
		QDomElement tlv=addElement(doc, root, "TServInfo", "");
		addElement(doc, tlv, "ServiceNum", QString::number(info.ServiceNum.value()));
		for (int i=0; i<info.ServiceNum.value(); i++)
		{
			addElement(doc, tlv, "Service", QString::number(info.Service[i].value()));
		}
	}	

	const Smpp::Tlv* fsstate = pdu->find_tlv(Smpp::Tlv::TFSState);
	if(fsstate)
	{
		Smpp::TFSState info;
		info.decode(fsstate->value(),fsstate->length());
		QDomElement tlv=addElement(doc, root, "TFSState", "");
		addElement(doc, tlv, "FSType", QString::number(info.FSType.value()));
		addElement(doc, tlv, "CCFSID", QString::number(info.CCFSID.value()));
	}	

	txt = doc.toString();
}

void MsgFactory::str_Logon_rsp(void *ptr, QString &txt)
{
	Smpp::LogonRsp *pdu = (Smpp::LogonRsp*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("LogonRsp");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	addElement(doc, root, "LogonID", QString::number(pdu->LogonID.value()));
	addElement(doc, root, "Seq", QString::number(pdu->Seq.value()));
	addElement(doc, root, "AcceptTime", QString::number(pdu->AcceptTime.value()));
	addElement(doc, root, "CCFSID", QString::number(pdu->CCFSID.value()));
	
	const Smpp::Tlv* ccfsInfo = pdu->find_tlv(Smpp::Tlv::TFSInfo);
	if(ccfsInfo)
	{
		Smpp::TFSInfo info;
		info.decode(ccfsInfo->value(),ccfsInfo->length());
		QDomElement tlv=addElement(doc, root, "TFSInfo", "");
		addElement(doc, tlv, "FSCode", info.FSCode.c_str());
	}	
	
	const Smpp::Tlv* ccfsServ = pdu->find_tlv(Smpp::Tlv::TServInfo);
	if(ccfsServ)
	{
		Smpp::TServInfo info;
		info.decode(ccfsServ->value(),ccfsServ->length());
		QDomElement tlv=addElement(doc, root, "TServInfo", "");
		addElement(doc, tlv, "ServiceNum", QString::number(info.ServiceNum.value()));
		for (int i=0; i<info.ServiceNum.value(); i++)
		{
			addElement(doc, tlv, "Service", QString::number(info.Service[i].value()));
		}
	}
	txt = doc.toString();
}
void MsgFactory::str_Logout_req(void *ptr, QString &txt)
{
	Smpp::LogoutReq *pdu = (Smpp::LogoutReq*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("LogoutReq");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	addElement(doc, root, "OriID", QString::number(pdu->OriID.value()));
	addElement(doc, root, "DstID", QString::number(pdu->DstID.value()));
	addElement(doc, root, "LogoutID", QString::number(pdu->LogoutID.value()));
	addElement(doc, root, "Seq", QString::number(pdu->Seq.value()));
	addElement(doc, root, "SubmitTime", QString::number(pdu->SubmitTime.value()));

	txt = doc.toString();
}

void MsgFactory::str_Logout_rsp(void *ptr, QString &txt)
{
	Smpp::LogoutRsp *pdu = (Smpp::LogoutRsp*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("LogoutRsp");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	addElement(doc, root, "LogoutID", QString::number(pdu->LogoutID.value()));
	addElement(doc, root, "Seq", QString::number(pdu->Seq.value()));
	addElement(doc, root, "AcceptTime", QString::number(pdu->AcceptTime.value()));

	txt = doc.toString();
}

void MsgFactory::str_Register_req(void *ptr, QString &txt)
{
	Smpp::RegisterReq *pdu = (Smpp::RegisterReq*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("RegisterReq");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	addElement(doc, root, "OriID", QString::number(pdu->OriID.value()));
	addElement(doc, root, "DstID", QString::number(pdu->DstID.value()));
	addElement(doc, root, "RegID", QString::number(pdu->RegID.value()));
	
	const Smpp::Tlv* msAddr = pdu->find_tlv(Smpp::Tlv::TBearAddr);
	if(msAddr)
	{
		Smpp::TBearAddr info;
		info.decode(msAddr->value(),msAddr->length());
		QDomElement tlv=addElement(doc, root, "TBearAddr", "");
		addElement(doc, tlv, "IPAddr", info.IPAddr.c_str());
		addElement(doc, tlv, "Port", QString::number(info.Port.value()));
	}	
	txt = doc.toString();

}

void MsgFactory::str_Register_rsp(void *ptr, QString &txt)
{
	Smpp::RegisterRsp *pdu = (Smpp::RegisterRsp*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("RegisterRsp");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	addElement(doc, root, "RegID", QString::number(pdu->RegID.value()));

	const Smpp::Tlv* msInfo = pdu->find_tlv(Smpp::Tlv::TMSInfo);
	if(msInfo)
	{
		Smpp::TMSInfo info;
		info.decode(msInfo->value(),msInfo->length());
		QDomElement tlv=addElement(doc, root, "TMSInfo", "");
		addElement(doc, tlv, "LogonTime", info.LogonTime.c_str());
		addElement(doc, tlv, "MSCode", info.MSCode.c_str());
		addElement(doc, tlv, "MSType", info.MSType.c_str());
		addElement(doc, tlv, "OriArea", info.OriArea.c_str());
		addElement(doc, tlv, "DstArea", info.DstArea.c_str());
	}	

	const Smpp::Tlv* msServ = pdu->find_tlv(Smpp::Tlv::TServInfo);
	if(msServ)
	{
		Smpp::TServInfo info;
		info.decode(msServ->value(),msServ->length());
		QDomElement tlv=addElement(doc, root, "TServInfo", "");
		addElement(doc, tlv, "ServiceNum", QString::number(info.ServiceNum.value()));
		for (int i=0; i<info.ServiceNum.value(); i++)
		{
			addElement(doc, tlv, "Service", QString::number(info.Service[i].value()));
		}
	}	
	const Smpp::Tlv* msSubscription = pdu->find_tlv(Smpp::Tlv::TMSSubscription);
	if(msSubscription)
	{
		Smpp::TMSSubscription info;
		info.decode(msSubscription->value(),msSubscription->length());
		QDomElement tlv=addElement(doc, root, "TMSSubscription", "");
		addElement(doc, tlv, "MSID", QString::number(info.MSID.value()));
		addElement(doc, tlv, "FSIDNum", QString::number(info.FSIDNum.value()));
		for (int i=0; i<info.FSIDNum.value(); i++)
		{
			addElement(doc, tlv, "FSID", QString::number(info.FSID[i].value()));
		}
	}	
	txt = doc.toString();

}

void MsgFactory::str_UnRegister_req(void *ptr, QString &txt)
{
	Smpp::UnRegisterReq *pdu = (Smpp::UnRegisterReq*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("UnRegisterReq");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	addElement(doc, root, "OriID", QString::number(pdu->OriID.value()));
	addElement(doc, root, "DstID", QString::number(pdu->DstID.value()));
	addElement(doc, root, "UnRegID", QString::number(pdu->UnRegID.value()));
	txt = doc.toString();

}

void MsgFactory::str_UnRegister_rsp(void *ptr, QString &txt)
{
	Smpp::UnRegisterRsp *pdu = (Smpp::UnRegisterRsp*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("UnRegisterRsp");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	
	txt = doc.toString();

}

void MsgFactory::str_Notify_req(void *ptr, QString &txt)
{
	Smpp::NotifyReq *pdu = (Smpp::NotifyReq*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("NotifyReq");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	addElement(doc, root, "OriID", QString::number(pdu->OriID.value()));
	addElement(doc, root, "DstID", QString::number(pdu->DstID.value()));
	addElement(doc, root, "NotifyID", QString::number(pdu->NotifyID.value()));
	addElement(doc, root, "SyncTag", QString::number(pdu->SyncTag.value()));

	const Smpp::Tlv* statuesInfo = pdu->find_tlv(Smpp::Tlv::TStatuesInfo);
	if(statuesInfo)
	{
		Smpp::TStatuesInfo info;
		info.decode(statuesInfo->value(),statuesInfo->length());
		QDomElement tlv=addElement(doc, root, "TStatuesInfo", "");
		addElement(doc, tlv, "Action", QString::number(info.Action.value()));

		Smpp::TlvList fsdlist=info.find_tlv_list(Smpp::Tlv::TFSDriscription);
		for(Smpp::TlvList::iterator i =fsdlist.begin(); i != fsdlist.end(); i++)
		{
			const Smpp::Tlv* fsDescription = *i;
			if(fsDescription)
			{
				Smpp::TFSDriscription info_fsd;
				info_fsd.decode(fsDescription->value(),fsDescription->length());
				QDomElement tlv2=addElement(doc, tlv, "TFSDriscription", "");
				addElement(doc, tlv2, "FSID", QString::number(info_fsd.FSID.value()));
				
				const Smpp::Tlv* fsinfo = info_fsd.find_tlv(Smpp::Tlv::TFSInfo);
				if(fsinfo)
				{
					Smpp::TFSInfo info_fsi;
					info_fsi.decode(fsinfo->value(),fsinfo->length());
					QDomElement tlv3=addElement(doc, tlv2, "TFSInfo", "");
					addElement(doc, tlv3, "FSCode", info_fsi.FSCode.c_str());
				}	
				
				const Smpp::Tlv* msAddr = info_fsd.find_tlv(Smpp::Tlv::TBearAddr);
				if(msAddr)
				{
					Smpp::TBearAddr info_bear;
					info_bear.decode(msAddr->value(),msAddr->length());
					QDomElement tlv3=addElement(doc, tlv2, "TBearAddr", "");
					addElement(doc, tlv3, "IPAddr", info_bear.IPAddr.c_str());
					addElement(doc, tlv3, "Port", QString::number(info_bear.Port.value()));
				}	
				
				const Smpp::Tlv* servinfo = info_fsd.find_tlv(Smpp::Tlv::TServInfo);
				if(servinfo)
				{
					Smpp::TServInfo info_serv;
					info_serv.decode(servinfo->value(),servinfo->length());
					QDomElement tlv3=addElement(doc, tlv2, "TServInfo", "");
					addElement(doc, tlv3, "ServiceNum", QString::number(info_serv.ServiceNum.value()));
					for (int i=0; i<info_serv.ServiceNum.value(); i++)
					{
						addElement(doc, tlv3, "Service", QString::number(info_serv.Service[i].value()));
					}
				}	
				
				const Smpp::Tlv* fsstate = info_fsd.find_tlv(Smpp::Tlv::TFSState);
				if(fsstate)
				{
					Smpp::TFSState info_state;
					info_state.decode(fsstate->value(),fsstate->length());
					QDomElement tlv3=addElement(doc, tlv2, "TFSState", "");
					addElement(doc, tlv3, "FSType", QString::number(info_state.FSType.value()));
					addElement(doc, tlv3, "CCFSID", QString::number(info_state.CCFSID.value()));
				}	
			}	
		}
		Smpp::TlvList msdlist=info.find_tlv_list(Smpp::Tlv::TMSDriscription);
		for(Smpp::TlvList::iterator i =msdlist.begin(); i != msdlist.end(); i++)
		{
			const Smpp::Tlv* msDescription = *i;
			if(msDescription)
			{
				Smpp::TMSDriscription info_msd;
				info_msd.decode(msDescription->value(),msDescription->length());
				QDomElement tlv2=addElement(doc, tlv, "TMSDriscription", "");
				addElement(doc, tlv2, "MSID", QString::number(info_msd.MSID.value()));
				
				const Smpp::Tlv* msinfo = info_msd.find_tlv(Smpp::Tlv::TMSInfo);
				if(msinfo)
				{
					Smpp::TMSInfo info_msi;
					info_msi.decode(msinfo->value(),msinfo->length());
					QDomElement tlv3=addElement(doc, tlv2, "TMSInfo", "");
					addElement(doc, tlv3, "LogonTime", info_msi.LogonTime.c_str());
					addElement(doc, tlv3, "MSCode", info_msi.MSCode.c_str());
					addElement(doc, tlv3, "MSType", info_msi.MSType.c_str());
					addElement(doc, tlv3, "OriArea", info_msi.OriArea.c_str());
					addElement(doc, tlv3, "DstArea", info_msi.DstArea.c_str());
				}	
				
				const Smpp::Tlv* msAddr = info_msd.find_tlv(Smpp::Tlv::TBearAddr);
				if(msAddr)
				{
					Smpp::TBearAddr info_bear;
					info_bear.decode(msAddr->value(),msAddr->length());
					QDomElement tlv3=addElement(doc, tlv2, "TBearAddr", "");
					addElement(doc, tlv3, "IPAddr", info_bear.IPAddr.c_str());
					addElement(doc, tlv3, "Port", QString::number(info_bear.Port.value()));
				}	
				
				const Smpp::Tlv* servinfo = info_msd.find_tlv(Smpp::Tlv::TServInfo);
				if(servinfo)
				{
					Smpp::TServInfo info_serv;
					info_serv.decode(servinfo->value(),servinfo->length());
					QDomElement tlv3=addElement(doc, tlv2, "TServInfo", "");
					addElement(doc, tlv3, "ServiceNum", QString::number(info_serv.ServiceNum.value()));
					for (int i=0; i<info_serv.ServiceNum.value(); i++)
					{
						addElement(doc, tlv3, "Service", QString::number(info_serv.Service[i].value()));
					}
				}	
				
				const Smpp::Tlv* msstate = info_msd.find_tlv(Smpp::Tlv::TMSState);
				if(msstate)
				{
					Smpp::TMSState info_state;
					info_state.decode(msstate->value(),msstate->length());
					QDomElement tlv3=addElement(doc, tlv2, "TMSState", "");
					addElement(doc, tlv3, "CurMSCID", QString::number(info_state.CurMSCID.value()));
					addElement(doc, tlv3, "MSState", QString::number(info_state.MSState.value()));
				}	
			}
		}
	}	

	txt = doc.toString();

}

void MsgFactory::str_Notify_rsp(void *ptr, QString &txt)
{
	Smpp::NotifyRsp *pdu = (Smpp::NotifyRsp*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("NotifyRsp");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));

	const Smpp::Tlv* msDescription = pdu->find_tlv(Smpp::Tlv::TMSDriscription);
	if(msDescription)
	{
		Smpp::TMSDriscription info_msd;
		info_msd.decode(msDescription->value(),msDescription->length());
		QDomElement tlv2=addElement(doc, root, "TMSDriscription", "");
		addElement(doc, tlv2, "MSID", QString::number(info_msd.MSID.value()));
		
		const Smpp::Tlv* msinfo = info_msd.find_tlv(Smpp::Tlv::TMSInfo);
		if(msinfo)
		{
			Smpp::TMSInfo info_msi;
			info_msi.decode(msinfo->value(),msinfo->length());
			QDomElement tlv3=addElement(doc, tlv2, "TMSInfo", "");
			addElement(doc, tlv3, "LogonTime", info_msi.LogonTime.c_str());
			addElement(doc, tlv3, "MSCode", info_msi.MSCode.c_str());
			addElement(doc, tlv3, "MSType", info_msi.MSType.c_str());
			addElement(doc, tlv3, "OriArea", info_msi.OriArea.c_str());
			addElement(doc, tlv3, "DstArea", info_msi.DstArea.c_str());
		}	
		
		const Smpp::Tlv* msAddr = info_msd.find_tlv(Smpp::Tlv::TBearAddr);
		if(msAddr)
		{
			Smpp::TBearAddr info_bear;
			info_bear.decode(msAddr->value(),msAddr->length());
			QDomElement tlv3=addElement(doc, tlv2, "TBearAddr", "");
			addElement(doc, tlv3, "IPAddr", info_bear.IPAddr.c_str());
			addElement(doc, tlv3, "Port", QString::number(info_bear.Port.value()));
		}	
		
		const Smpp::Tlv* servinfo = info_msd.find_tlv(Smpp::Tlv::TServInfo);
		if(servinfo)
		{
			Smpp::TServInfo info_serv;
			info_serv.decode(servinfo->value(),servinfo->length());
			QDomElement tlv3=addElement(doc, tlv2, "TServInfo", "");
			addElement(doc, tlv3, "ServiceNum", QString::number(info_serv.ServiceNum.value()));
			for (int i=0; i<info_serv.ServiceNum.value(); i++)
			{
				addElement(doc, tlv3, "Service", QString::number(info_serv.Service[i].value()));
			}
		}	
		
		const Smpp::Tlv* msstate = info_msd.find_tlv(Smpp::Tlv::TMSState);
		if(msstate)
		{
			Smpp::TMSState info_state;
			info_state.decode(msstate->value(),msstate->length());
			QDomElement tlv3=addElement(doc, tlv2, "TMSState", "");
			addElement(doc, tlv3, "CurMSCID", QString::number(info_state.CurMSCID.value()));
			addElement(doc, tlv3, "MSState", QString::number(info_state.MSState.value()));
		}	
	}	

	const Smpp::Tlv* fsDescription = pdu->find_tlv(Smpp::Tlv::TFSDriscription);
	if(fsDescription)
	{
		Smpp::TFSDriscription info_fsd;
		info_fsd.decode(fsDescription->value(),fsDescription->length());
		QDomElement tlv2=addElement(doc, root, "TFSDriscription", "");
		addElement(doc, tlv2, "FSID", QString::number(info_fsd.FSID.value()));
		
		const Smpp::Tlv* fsinfo = info_fsd.find_tlv(Smpp::Tlv::TFSInfo);
		if(fsinfo)
		{
			Smpp::TFSInfo info_fsi;
			info_fsi.decode(fsinfo->value(),fsinfo->length());
			QDomElement tlv3=addElement(doc, tlv2, "TFSInfo", "");
			addElement(doc, tlv3, "FSCode", info_fsi.FSCode.c_str());
		}	
		
		const Smpp::Tlv* msAddr = info_fsd.find_tlv(Smpp::Tlv::TBearAddr);
		if(msAddr)
		{
			Smpp::TBearAddr info_bear;
			info_bear.decode(msAddr->value(),msAddr->length());
			QDomElement tlv3=addElement(doc, tlv2, "TBearAddr", "");
			addElement(doc, tlv3, "IPAddr", info_bear.IPAddr.c_str());
			addElement(doc, tlv3, "Port", QString::number(info_bear.Port.value()));
		}	
		
		const Smpp::Tlv* servinfo = info_fsd.find_tlv(Smpp::Tlv::TServInfo);
		if(servinfo)
		{
			Smpp::TServInfo info_serv;
			info_serv.decode(servinfo->value(),servinfo->length());
			QDomElement tlv3=addElement(doc, tlv2, "TServInfo", "");
			addElement(doc, tlv3, "ServiceNum", QString::number(info_serv.ServiceNum.value()));
			for (int i=0; i<info_serv.ServiceNum.value(); i++)
			{
				addElement(doc, tlv3, "Service", QString::number(info_serv.Service[i].value()));
			}
		}	
		
		const Smpp::Tlv* fsstate = info_fsd.find_tlv(Smpp::Tlv::TFSState);
		if(fsstate)
		{
			Smpp::TFSState info_state;
			info_state.decode(fsstate->value(),fsstate->length());
			QDomElement tlv3=addElement(doc, tlv2, "TFSState", "");
			addElement(doc, tlv3, "FSType", QString::number(info_state.FSType.value()));
			addElement(doc, tlv3, "CCFSID", QString::number(info_state.CCFSID.value()));
		}	
	}	
	
	txt = doc.toString();

}

void MsgFactory::str_Handover_req(void *ptr, QString &txt)
{
	Smpp::HandoverReq *pdu = (Smpp::HandoverReq*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("HandoverReq");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	addElement(doc, root, "OriID", QString::number(pdu->OriID.value()));
	addElement(doc, root, "DstID", QString::number(pdu->DstID.value()));
	addElement(doc, root, "HandoverID", QString::number(pdu->HandoverID.value()));
	txt = doc.toString();

}

void MsgFactory::str_Handover_rsp(void *ptr, QString &txt)
{
	Smpp::HandoverRsp *pdu = (Smpp::HandoverRsp*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("HandoverRsp");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	addElement(doc, root, "HandoverID", QString::number(pdu->HandoverID.value()));
	txt = doc.toString();

}

void MsgFactory::str_Takeover_req(void *ptr, QString &txt)
{
	Smpp::TakeoverReq *pdu = (Smpp::TakeoverReq*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("TakeoverReq");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	addElement(doc, root, "OriID", QString::number(pdu->OriID.value()));
	addElement(doc, root, "DstID", QString::number(pdu->DstID.value()));
	addElement(doc, root, "TakeoverID", QString::number(pdu->TakeoverID.value()));

	const Smpp::Tlv* msSubscription = pdu->find_tlv(Smpp::Tlv::TMSSubscription);
	if(msSubscription)
	{
		Smpp::TMSSubscription info;
		info.decode(msSubscription->value(),msSubscription->length());
		QDomElement tlv=addElement(doc, root, "TMSSubscription", "");
		addElement(doc, tlv, "MSID", QString::number(info.MSID.value()));
		addElement(doc, tlv, "FSIDNum", QString::number(info.FSIDNum.value()));
		for (int i=0; i<info.FSIDNum.value(); i++)
		{
			addElement(doc, tlv, "FSID", QString::number(info.FSID[i].value()));
		}
	}	

	txt = doc.toString();

}


void MsgFactory::str_Takeover_rsp(void *ptr, QString &txt)
{
	Smpp::TakeoverRsp *pdu = (Smpp::TakeoverRsp*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("TakeoverRsp");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	addElement(doc, root, "TakeoverID", QString::number(pdu->TakeoverID.value()));
	txt = doc.toString();

}

void MsgFactory::str_Subscribe_req(void *ptr, QString &txt)
{
	Smpp::SubscribeReq *pdu = (Smpp::SubscribeReq*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("SubscribeReq");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	addElement(doc, root, "OriID", QString::number(pdu->OriID.value()));
	addElement(doc, root, "DstID", QString::number(pdu->DstID.value()));
	addElement(doc, root, "SubscribeID", QString::number(pdu->SubscribeID.value()));
	addElement(doc, root, "FSID", QString::number(pdu->FSID.value()));
	txt = doc.toString();

}

void MsgFactory::str_Subscribe_rsp(void *ptr, QString &txt)
{
	Smpp::SubscribeRsp *pdu = (Smpp::SubscribeRsp*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("SubscribeRsp");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	addElement(doc, root, "SubscribeID", QString::number(pdu->SubscribeID.value()));

	const Smpp::Tlv* msinfo = pdu->find_tlv(Smpp::Tlv::TMSInfo);
	if(msinfo)
	{
		Smpp::TMSInfo info;
		info.decode(msinfo->value(),msinfo->length());
		QDomElement tlv=addElement(doc, root, "TMSInfo", "");
		addElement(doc, tlv, "LogonTime", info.LogonTime.c_str());
		addElement(doc, tlv, "MSCode", info.MSCode.c_str());
		addElement(doc, tlv, "MSType", info.MSType.c_str());
		addElement(doc, tlv, "OriArea", info.OriArea.c_str());
		addElement(doc, tlv, "DstArea", info.DstArea.c_str());
	}
	
	const Smpp::Tlv* servinfo = pdu->find_tlv(Smpp::Tlv::TServInfo);
	if(servinfo)
	{
		Smpp::TServInfo info;
		info.decode(servinfo->value(),servinfo->length());
		QDomElement tlv=addElement(doc, root, "TServInfo", "");
		addElement(doc, tlv, "ServiceNum", QString::number(info.ServiceNum.value()));
		for (int i=0; i<info.ServiceNum.value(); i++)
		{
			addElement(doc, tlv, "Service", QString::number(info.Service[i].value()));
		}
	}	

	const Smpp::Tlv* msstate = pdu->find_tlv(Smpp::Tlv::TMSState);
	if(msstate)
	{
		Smpp::TMSState info;
		info.decode(msstate->value(),msstate->length());
		QDomElement tlv=addElement(doc, root, "TMSState", "");
		addElement(doc, tlv, "CurMSCID", QString::number(info.CurMSCID.value()));
		addElement(doc, tlv, "MSState", QString::number(info.MSState.value()));
	}	

	txt = doc.toString();

}

void MsgFactory::str_UnSubscribe_req(void *ptr, QString &txt)
{
	Smpp::UnSubscribeReq *pdu = (Smpp::UnSubscribeReq*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("UnSubscribeReq");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	addElement(doc, root, "OriID", QString::number(pdu->OriID.value()));
	addElement(doc, root, "DstID", QString::number(pdu->DstID.value()));
	addElement(doc, root, "SubscribeID", QString::number(pdu->SubscribeID.value()));
	addElement(doc, root, "FSID", QString::number(pdu->FSID.value()));
	txt = doc.toString();

}

void MsgFactory::str_UnSubscribe_rsp(void *ptr, QString &txt)
{
	Smpp::UnSubscribeRsp *pdu = (Smpp::UnSubscribeRsp*)ptr;
	if (!pdu)
		return;

	QDomDocument doc("MyML");
	QDomElement root = doc.createElement("UnSubscribeRsp");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	txt = doc.toString();

}

void MsgFactory::str_ForceLogout_req(void *ptr, QString &txt)
{
	Smpp::ForceLogoutReq *pdu = (Smpp::ForceLogoutReq*)ptr;
	if (!pdu)
		return;

	QDomDocument doc;//("MyML");
	QDomElement root = doc.createElement("ForceLogoutReq");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	addElement(doc, root, "OriID", QString::number(pdu->OriID.value()));
	addElement(doc, root, "DstID", QString::number(pdu->DstID.value()));
	addElement(doc, root, "SubscribeID", QString::number(pdu->SubscribeID.value()));
	addElement(doc, root, "FSID", QString::number(pdu->FSID.value()));
	txt = doc.toString();

}

void MsgFactory::str_ForceLogout_rsp(void *ptr, QString &txt)
{
	Smpp::ForceLogoutRsp *pdu = (Smpp::ForceLogoutRsp*)ptr;
	if (!pdu)
		return;

	QDomDocument doc("MyML");
	QDomElement root = doc.createElement("ForceLogoutRsp");
	doc.appendChild(root);

	addElement(doc, root, "SequenceNumber", QString::number(pdu->sequence_number()));
	addElement(doc, root, "CommandStatus", QString::number(pdu->command_status()));
	txt = doc.toString();

}

QDomElement& MsgFactory::addElement(QDomDocument &doc, QDomElement &parent, QString name, QString value)
{
	QDomElement tag = doc.createElement(name);
	parent.appendChild(tag);
	if (!value.isEmpty())
	{
		QDomText t = doc.createTextNode(value);
		tag.appendChild(t);
	}
	return tag;

}

QByteArray MsgFactory::encode(int type, void *ptr)
{
	QByteArray buff;
	switch (type)
	{
		case Smpp::CommandId::Logon_req:
			{
				Smpp::LogonReq *pdu = (Smpp::LogonReq*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::Logon_rsp:
			{
				Smpp::LogonRsp *pdu = (Smpp::LogonRsp*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::Logout_req:
			{
				Smpp::LogoutReq *pdu = (Smpp::LogoutReq*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::Logout_rsp:
			{
				Smpp::LogoutRsp *pdu = (Smpp::LogoutRsp*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::Register_req:
			{
				Smpp::RegisterReq *pdu = (Smpp::RegisterReq*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::Register_rsp:
			{
				Smpp::RegisterRsp *pdu = (Smpp::RegisterRsp*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::UnRegister_req:				
			{
				Smpp::UnRegisterReq *pdu = (Smpp::UnRegisterReq*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::UnRegister_rsp:				
			{
				Smpp::UnRegisterRsp *pdu = (Smpp::UnRegisterRsp*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::Notify_req:
			{
				Smpp::NotifyReq *pdu = (Smpp::NotifyReq*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::Notify_rsp:
			{
				Smpp::NotifyRsp *pdu = (Smpp::NotifyRsp*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::Handover_req:
			{
				Smpp::HandoverReq *pdu = (Smpp::HandoverReq*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::Handover_rsp:
			{
				Smpp::HandoverRsp *pdu = (Smpp::HandoverRsp*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::Takeover_req:
			{
				Smpp::TakeoverReq *pdu = (Smpp::TakeoverReq*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::Takeover_rsp:
			{
				Smpp::TakeoverRsp *pdu = (Smpp::TakeoverRsp*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::Subscribe_req:
			{
				Smpp::SubscribeReq *pdu = (Smpp::SubscribeReq*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::Subscribe_rsp:
			{
				Smpp::SubscribeRsp *pdu = (Smpp::SubscribeRsp*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::UnSubscribe_req:
			{
				Smpp::UnSubscribeReq *pdu = (Smpp::UnSubscribeReq*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::UnSubscribe_rsp:
			{
				Smpp::UnSubscribeRsp *pdu = (Smpp::UnSubscribeRsp*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::ForceLogout_req:
			{
				Smpp::ForceLogoutReq *pdu = (Smpp::ForceLogoutReq*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
		case Smpp::CommandId::ForceLogout_rsp:
			{
				Smpp::ForceLogoutRsp *pdu = (Smpp::ForceLogoutRsp*)ptr;
				buff.append((char*)pdu->encode(),pdu->command_length());
				break;
			}
	}

	return buff;
}

void* MsgFactory::decode(int type, QByteArray buff)
{
	switch (type)
	{
		case Smpp::CommandId::Logon_req:
			{
				Smpp::LogonReq *pdu = new Smpp::LogonReq;
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::Logon_rsp:
			{
				Smpp::LogonRsp *pdu = new (Smpp::LogonRsp);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::Logout_req:
			{
				Smpp::LogoutReq *pdu = new (Smpp::LogoutReq);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::Logout_rsp:
			{
				Smpp::LogoutRsp *pdu = new (Smpp::LogoutRsp);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::Register_req:
			{
				Smpp::RegisterReq *pdu = new (Smpp::RegisterReq);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::Register_rsp:
			{
				Smpp::RegisterRsp *pdu = new (Smpp::RegisterRsp);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::UnRegister_req:				
			{
				Smpp::UnRegisterReq *pdu = new (Smpp::UnRegisterReq);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::UnRegister_rsp:				
			{
				Smpp::UnRegisterRsp *pdu = new (Smpp::UnRegisterRsp);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::Notify_req:
			{
				Smpp::NotifyReq *pdu = new (Smpp::NotifyReq);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::Notify_rsp:
			{
				Smpp::NotifyRsp *pdu = new (Smpp::NotifyRsp);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::Handover_req:
			{
				Smpp::HandoverReq *pdu = new (Smpp::HandoverReq);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::Handover_rsp:
			{
				Smpp::HandoverRsp *pdu = new (Smpp::HandoverRsp);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::Takeover_req:
			{
				Smpp::TakeoverReq *pdu = new (Smpp::TakeoverReq);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::Takeover_rsp:
			{
				Smpp::TakeoverRsp *pdu = new (Smpp::TakeoverRsp);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::Subscribe_req:
			{
				Smpp::SubscribeReq *pdu = new (Smpp::SubscribeReq);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::Subscribe_rsp:
			{
				Smpp::SubscribeRsp *pdu = new (Smpp::SubscribeRsp);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::UnSubscribe_req:
			{
				Smpp::UnSubscribeReq *pdu = new (Smpp::UnSubscribeReq);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::UnSubscribe_rsp:
			{
				Smpp::UnSubscribeRsp *pdu = new Smpp::UnSubscribeRsp;
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::ForceLogout_req:
			{
				Smpp::ForceLogoutReq *pdu = new (Smpp::ForceLogoutReq);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
		case Smpp::CommandId::ForceLogout_rsp:
			{
				Smpp::ForceLogoutRsp *pdu = new (Smpp::ForceLogoutRsp);
				pdu->decode((Smpp::Uint8*)buff.data());
				return pdu;
			}
	}

	return NULL;
}

char* MsgFactory::strmsgname(int type)
{
	switch (type)
	{
		case Smpp::CommandId::Logon_req:
			return "LogonReq";
		case Smpp::CommandId::Logon_rsp:
			return "LogonRsp";
		case Smpp::CommandId::Logout_req:
			return "LogoutReq";
		case Smpp::CommandId::Logout_rsp:
			return "LogoutRsp";
		case Smpp::CommandId::Register_req:
			return "RegisterReq";
		case Smpp::CommandId::Register_rsp:
			return "RegisterRsp";
		case Smpp::CommandId::UnRegister_req:				
			return "UnRegisterReq";
		case Smpp::CommandId::UnRegister_rsp:				
			return "UnRegisterRsp";
		case Smpp::CommandId::Notify_req:
			return "NotifyReq";
		case Smpp::CommandId::Notify_rsp:
			return "NotifyRsp";
		case Smpp::CommandId::Handover_req:
			return "HandoverReq";
		case Smpp::CommandId::Handover_rsp:
			return "HandoverRsp";
		case Smpp::CommandId::Takeover_req:
			return "TakeoverReq";
		case Smpp::CommandId::Takeover_rsp:
			return "TakeoverRsp";
		case Smpp::CommandId::Subscribe_req:
			return "SubscribeReq";
		case Smpp::CommandId::Subscribe_rsp:
			return "SubscribeRsp";
		case Smpp::CommandId::UnSubscribe_req:
			return "UnSubscribeReq";
		case Smpp::CommandId::UnSubscribe_rsp:
			return "UnSubscribeRsp";
		case Smpp::CommandId::ForceLogout_req:
			return "ForceLogoutReq";
		case Smpp::CommandId::ForceLogout_rsp:
			return "ForceLogoutRsp";
	}

	return "";
}

int MsgFactory::msgnametostr(QString name)
{
	if (name == "LogonReq")
		return Smpp::CommandId::Logon_req;
	if (name == "LogonRsp")
		return Smpp::CommandId::Logon_rsp;
	if (name == "LogoutReq")
		return Smpp::CommandId::Logout_req;
	if (name == "LogoutRsp")
		return Smpp::CommandId::Logout_rsp;
	if (name == "RegisterReq")
		return Smpp::CommandId::Register_req;
	if (name == "RegisterRsp")
		return Smpp::CommandId::Register_rsp;
	if (name == "UnRegisterReq")
		return Smpp::CommandId::UnRegister_req;				
	if (name == "UnRegisterRsp")
		return Smpp::CommandId::UnRegister_rsp;		
	if (name == "NotifyReq")
		return Smpp::CommandId::Notify_req;
	if (name == "NotifyRsp")
		return Smpp::CommandId::Notify_rsp;
	if (name == "HandoverReq")
		return Smpp::CommandId::Handover_req;
	if (name == "HandoverRsp")
		return Smpp::CommandId::Handover_rsp;
	if (name == "TakeoverReq")
		return Smpp::CommandId::Takeover_req;
	if (name == "TakeoverRsp")
		return Smpp::CommandId::Takeover_rsp;
	if (name == "SubscribeReq")
		return Smpp::CommandId::Subscribe_req;
	if (name == "SubscribeRsp")
		return Smpp::CommandId::Subscribe_rsp;
	if (name == "UnSubscribeReq")
		return Smpp::CommandId::UnSubscribe_req;
	if (name == "UnSubscribeRsp")
		return Smpp::CommandId::UnSubscribe_rsp;
	if (name == "ForceLogoutReq")
		return Smpp::CommandId::ForceLogout_req;
	if (name == "ForceLogoutRsp")
		return Smpp::CommandId::ForceLogout_rsp;
	else
		return 0;
}


